package com.example.meetdroid.chat;



import java.util.ArrayList;
import java.util.List;

import com.example.meetdroid.ActivityChat;
import com.example.meetdroid.chat.ChatMessage;
import com.example.meetdroid.R;
import com.example.meetdroid.main.Globals;
import com.example.meetdroid.main.User;

import android.app.Activity;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.TextView;

public class ChatListAdapter extends ArrayAdapter<ChatMessage> {

	protected static final String LOG_TAG = ChatListAdapter.class.getSimpleName();
	
	private List<ChatMessage> items;
	private int layoutResourceIdSend;
	private int layoutResourceIdReceived;
	private int layOutTextViewRID;
	private Context context;

	public ChatListAdapter(	Context context, int layoutResourceIdSend,
							int layoutResourceIdReceived, List<ChatMessage> items) {
		
		super(context,layoutResourceIdSend, items);
		this.layoutResourceIdSend = layoutResourceIdSend;
		this.layoutResourceIdReceived = layoutResourceIdReceived;
		this.context = context;
		this.items = items;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View row = convertView;
		ChatHolder holder = null;
		
		LayoutInflater inflater = ((Activity) context).getLayoutInflater();
		
		holder = new ChatHolder();
		holder.cMessage = items.get(position);
		holder.cTxtViewMessage = (TextView)row.findViewById(this.layOutTextViewRID);
		if (holder.cMessage.getUser().getID() == Globals.CurrentUser.getID())
			row = inflater.inflate(layoutResourceIdSend, parent, false);
		else
			row = inflater.inflate(layoutResourceIdReceived, parent, false);
		
		row.setTag(holder);
		setupItem(holder);
		return row;
	}

	private void setupItem(ChatHolder holder) {
		holder.cTxtViewMessage.setText(holder.cMessage.getMessage());
	}

	public static class ChatHolder {
		ChatMessage cMessage;
		TextView cTxtViewMessage;
	}
	

}
